#include "ScnsEspAtUart.c.h"

#if defined(SCNS_ESP_AT_UART_ENABLE)&&SCNS_ESP_AT_UART_ENABLE==1

#include "usart.h"
#include "ScnsSmctpExtLogger.h"
#include "ScnsMenu.h"
#include "rtc.h"

#if defined(__has_include)
#if __has_include ("hxMqttConfig.h")

#include "hxMqttConfig.h"
#include "hxMqtt.h"

#endif
#endif

const ScnsBspEspAtUartConfig scnsBspEspAtUartConfig[SCNS_ESP_AT_UART_MAX]={
    {
        .station={
            .ssid="DoYouLikeJuruoyun",
            .password="ILikeJuruoyun",
        },
        .tcpClient={
#if defined(HX_MQTT_SERVER)&&defined(HX_MQTT_PORT)
            .protocol="SSL",
            .ip=HX_MQTT_SERVER,
            .port=HX_MQTT_PORT,
#else
            .ip="192.168.137.1",
            .port=1000,
#endif
        },
    },
};

void scnsBspEspAtUartSetEn(ScnsEspAtUartEnum espN,uint8 dat)
{
    ESP_CS_GPIO_Port->BSRR=(dat)?(ESP_CS_Pin):(ESP_CS_Pin<<16);
}

void scnsBspEspAtUartSetRst(ScnsEspAtUartEnum espN,uint8 dat)
{
    ESP_RST_GPIO_Port->BSRR=(dat)?(ESP_RST_Pin):(ESP_RST_Pin<<16);

}

void scnsBspEspAtUartSend(ScnsEspAtUartEnum espN,const uint8*buf,uint16*len)
{
    HAL_UART_Transmit_DMA(&huart3,(uint8*)buf,*len);
}

void scnsBspEspAtUartReceiveIdleDmaStart(ScnsEspAtUartEnum espN,uint8*buf,uint32 len)
{
    HAL_UARTEx_ReceiveToIdle_DMA(&huart3,buf,len);
}

void scnsBspEspAtUartReceiveIdleDmaStop(ScnsEspAtUartEnum espN,uint8*buf,uint32 len)
{
    HAL_UART_AbortReceive(&huart3);
}

void scnsBspEspAtUartWaiting(ScnsEspAtUartEnum espN)
{
#if defined(SCNS_LCD_ENABLE)&&SCNS_LCD_ENABLE==1&&defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1&&defined(SCNS_SMCTP_EXT_LOGGER_ENABLE)&&SCNS_SMCTP_EXT_LOGGER_ENABLE==1
    scnsSmctpExtLoggerDisplay(SCNS_LCD_LAYER_AUTO,(Point){0,scnsLcdGetBorder().y-scnsLcdFontConsolas10.fontHeight*6},6,scnsLcdGetBorder().x,0,6,&scnsLcdFontConsolas10,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);
#endif
#if defined(SCNS_MENU_ENABLE)&&SCNS_MENU_ENABLE==1
    scnsMenuFresh();
#endif
}

void scnsBspEspAtUartStationConnected(ScnsEspAtUartEnum espN,uint8*ip)
{
    hxPrintfFL("wifi connected %s\n",ip);
}

void scnsBspEspAtUartPathThroughReceive(ScnsEspAtUartEnum espN,const uint8*buf,uint16 len)
{
    hxMqttRecv(buf,len);
}

void scnsBspEspAtUartGotSNTP(ScnsEspAtUartEnum espN,uint16 year,uint8 month,uint8 day,uint8 hour,uint8 minute,uint8 second)
{
    RTC_TimeTypeDef rtcTime={.Hours=hour,.Minutes=minute,.Seconds=second};
    RTC_DateTypeDef rtcDate={.Date=day,.Month=month,.Year=year};
    HAL_RTC_SetTime(&hrtc,&rtcTime,RTC_FORMAT_BIN);
    HAL_RTC_SetDate(&hrtc,&rtcDate,RTC_FORMAT_BIN);
    (void)rtcTime;
    (void)rtcDate;
}

#endif
